import pymysql


class Connect:
    def __init__(self):
        # 在这里连接数据库和创建游标
        self.conn = pymysql.connect(
            host='140.143.206.157',
            port=3306,
            user='root',
            password='123456',
            db='AB',
            charset='utf8'
        )
        self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

    def closer(self):
        # 关闭连接和关闭游标
        self.cursor.close()
        self.conn.close()

    def __enter__(self):
        # with方法的enter函数
        return self

    def __exit__(self, exc_type, exc_value, exc_tb):
        # with方法的exit函数
        print(exc_type)
        print(exc_value)
        print(exc_tb)
        self.closer()
        return True
